const svc = require('../util/service'),
http = require("http"),
url = require("url"),
log = require("../util/log");

module.exports = (req, res) => {
	svc.handlePost(req, res, (obj, resolve) => {
		if (!obj.url) {
			resolve({
				result : -1,
				msg : '缺少url'
			})
			return
		}
		const urlObj = url.parse(obj.url, true);
		var options = {
			host : urlObj.hostname,
			port : urlObj.port,
			path : urlObj.pathname,
			method : 'GET'
		}
		if (urlObj.search)
			options.path += urlObj.search;
		const req = http.request(options, (res) => {
				var body = []
				res.on('data', (chunk) => {
					body.push(chunk)
				}).on('end', () => {
					var resObj = JSON.parse(Buffer.concat(body).toString());
					resolve(resObj)
				}).on('error', (err) => {
					log(err)
				})
			});
		req.on('error', (err) => {
			log(err)
		})
		req.end()
	})
}
